<?php

namespace App\Kuafu\Mybank\Resources;

use App\Http\Resources\BaseResource;
use App\Kuafu\Mybank\Model\MybankEntryModel;
use App\Remote\Mybank\Remote;

class MybankEntryDetailResource extends BaseResource
{
    public function toArray($request): array
    {
        return [
            "id"                         => $this->id,
            "outOrderNo"                 => $this->outOrderNo,
            "orderNo"                    => $this->orderNo,
            "outMerchantId"              => $this->outMerchantId,
            "type"                       => $this->type,
            "typeText"                   => translateEnumCodeToCn(MybankEntryModel::TYPE_TEXT, $this->type),
            "merchantId"                 => $this->merchantId,
            "merchantType"               => $this->merchantType,
            "merchantTypeText"           => translateEnumCodeToCn(MybankEntryModel::MERCHANT_TYPE_TEXT,
                $this->merchantType),
            "merchantName"               => $this->merchantName,
            "contactMobile"              => $this->contactMobile,
            "contactName"                => $this->contactName,
            "principalMobile"            => $this->principalMobile,
            "principalCertType"          => $this->principalCertType,
            "principalCertNo"            => $this->principalCertNo,
            "principalPerson"            => $this->principalPerson,
            "bussAuthType"               => $this->bussAuthType,
            "bussAuthNo"                 => $this->bussAuthNo,
            "bussAuthVld"                => $this->bussAuthVld,
            "status"                     => $this->status,
            "statusText"                 => translateEnumCodeToCn(MybankEntryModel::STATUS_TEXT, $this->status),
            "withholdStatus"             => $this->withholdStatus,
            "withholdStatusText"         => translateEnumCodeToCn(MybankEntryModel::WITHHOLD_STATUS_TEXT,
                $this->withholdStatus),
            "withholdOpenAt"             => formatDate($this->withholdOpenAt),
            "withholdCloseAt"            => formatDate($this->withholdCloseAt),
            "createdAt"                  => formatDate($this->createdAt->timestamp),
            "updatedAt"                  => formatDate($this->updatedAt->timestamp),
            "shopBindList"               => $this->shopBindRel->map(function ($v) {
                return [
                    "id"       => $v->shopRel->id,
                    "name"     => $v->shopRel->name,
                    "shopCode" => $v->shopRel->shopCode,
                ];
            }),
            "activeUrl"                  => self::getActiveUrl($this->status, $this->registerStatus,
                $this->outMerchantId, $this->principalMobile),
            "preRegisterStatus"          => $this->preRegisterStatus,
            "preRegisterStatusText"      => translateEnumCodeToCn(MybankEntryModel::PRE_REGISTER_STATUS_TEXT,
                $this->preRegisterStatus),
            "registerStatus"             => $this->registerStatus,
            "registerStatusText"         => translateEnumCodeToCn(MybankEntryModel::REGISTER_STATUS_TEXT,
                $this->registerStatus),
            "identificationCardLife"     => $this->identificationCardLife,
            "identificationCardBackImg"  => tigerPrivateFilePath($this->identificationCardBackImg),
            "identificationCardFrontImg" => tigerPrivateFilePath($this->identificationCardFrontImg),
            "businessLicenseImg"         => tigerPicturePath($this->businessLicenseImg),
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * @param int $status
     * @param int $registerStatus
     * @param string $outMerchantId
     * @param string $mobile
     * @return string|null
     */
    public static function getActiveUrl(
        int $status,
        int $registerStatus,
        string $outMerchantId,
        string $mobile
    ): ?string {
        if ($status == MybankEntryModel::STATUS_2 && $registerStatus == MybankEntryModel::REGISTER_STATUS_0) {
            if (isTest()) {
                $appId    = 2021002115660005;
                $isvOrgId = Remote::getIsvOrgId();
                $uid      = MybankEntryDetailResource::getTestUidConfig($mobile);
                return "alipays://platformapi/startapp?appId={$appId}&nbupdate=syncforce&query=outMchId%3D{$outMerchantId}%26isv%3D{$isvOrgId}%26app%3Dbkclfs%26t%3DREGISTER%26uid%3D{$uid}";
            } else {
                $appId    = Remote::getAppid();
                $isvOrgId = Remote::getIsvOrgId();
                return "alipays://platformapi/startapp?appId=2021001187624111&nbupdate=syncforce&query=outMchId%3D{$outMerchantId}%26isv%3D{$isvOrgId}%26app%3Dbkclfs%26t%3DREGISTER";
            }
        }
        return null;
    }


    /**
     * 3 商户登录接口
     * 登录时无需先与网商后端交互，直接替换下述链接中merchantId为网商为平台下的商户分配的网商商户号；ISV_ORG_ID替换为网商分配的平台号；通过前端生成二维码，用户使用支付宝扫码之后即可进入网商云资金小程序商户首页：
     * @param string $mchId
     * @return string
     */
    public static function getLoginUrl(string $mchId): string
    {
        if (isTest()) {
            $appId    = 2021002115660005;
            $isvOrgId = Remote::getIsvOrgId();

            $merchantInfo = MybankEntryModel::query()
                                            ->where('merchantId', $mchId)
                                            ->first();
            $uid          = MybankEntryDetailResource::getTestUidConfig($merchantInfo->principalMobile);
            return "alipays://platformapi/startapp?appId={$appId}&nbupdate=syncforce&query=mchId%3D{$mchId}%26isv%3D{$isvOrgId}%26app%3Dbkclfs%26t%3DLOGIN%26uid%3D{$uid}";
        } else {
            $appId    = Remote::getAppid();
            $isvOrgId = Remote::getIsvOrgId();
            return "alipays://platformapi/startapp?appId=2021001187624111&nbupdate=syncforce&query=mchId%3D{$mchId}%26isv%3D{$isvOrgId}%26app%3Dbkclfs%26t%3DLOGIN";
        }
    }

    /**
     * 测试账号 UID
     *
     * @param string $mobile
     * @return string
     */
    public static function getTestUidConfig(string $mobile): string
    {
        $uidConfig = [
            "15802713829" => "2088502337851303",
            "14450763299" => "2088502337851361",
            "17005352050" => "2088502337826462",
            "16210072227" => "2088502450683119",
        ];
        return $uidConfig[$mobile] ?? '';
    }
}
